package cn.tedu.egaddress1.controller;

import cn.tedu.egaddress1.mapper.AddressMapper;
import cn.tedu.egaddress1.pojo.dto.AddressAddParam;
import cn.tedu.egaddress1.pojo.entity.Address;
import cn.tedu.egaddress1.pojo.vo.AddressInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/address")
public class Addcontroller {

    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("add")
    public String add(@RequestBody AddressAddParam addressAddParam){
    System.out.println("addressAddParam = " + addressAddParam);
    Address address = new Address();
    BeanUtils.copyProperties(addressAddParam,address);
    address.setId(1L);
    addressMapper.insertAddress(address);
    return "添加地址成功";
    }

    @GetMapping("list")
    public List<AddressInfoVO> list(){

        List<AddressInfoVO> addressInfoVO = addressMapper.selectAddressList();
        return addressInfoVO;
    }
    
    @DeleteMapping("del")
    public String del(Long id){
        System.out.println("id="+id);
        int count = addressMapper.deleteAddress(id);
        if (count>0){
            return "删除地址成功";
        }
        return "删除地址失败";
    }
}
